/**
 * 平台检测工具
 * 用于统一管理微信小程序、安卓App、H5三端的平台差异
 */

// 获取当前平台信息
export function getPlatform() {
  const systemInfo = uni.getSystemInfoSync()
  return {
    platform: systemInfo.platform,
    isWechatMP: process.env.UNI_PLATFORM === 'mp-weixin',
    isApp: process.env.UNI_PLATFORM === 'app-plus',
    isH5: process.env.UNI_PLATFORM === 'h5',
    isDev: process.env.NODE_ENV === 'development'
  }
}

// 微信小程序专用方法
export function wechatMPOnly(fn) {
  // #ifdef MP-WEIXIN
  return fn()
  // #endif
  // #ifndef MP-WEIXIN
  return null
  // #endif
}

// App专用方法
export function appOnly(fn) {
  // #ifdef APP-PLUS
  return fn()
  // #endif
  // #ifndef APP-PLUS
  return null
  // #endif
}

// H5专用方法
export function h5Only(fn) {
  // #ifdef H5
  return fn()
  // #endif
  // #ifndef H5
  return null
  // #endif
}

// 通用文件选择器
export function chooseFile(options = {}) {
  const { count = 1, type = 'all', extension = [] } = options
  
  return new Promise((resolve, reject) => {
    // #ifdef MP-WEIXIN
    uni.chooseMessageFile({
      count,
      type,
      extension,
      success: resolve,
      fail: reject
    })
    // #endif
    
    // #ifdef APP-PLUS
    plus.gallery.pick((path) => {
      resolve({
        tempFiles: [{
          path: path,
          name: path.split('/').pop(),
          size: 0
        }]
      })
    }, reject, {
      filter: type === 'image' ? 'image' : 'video',
      multiple: count > 1
    })
    // #endif
    
    // #ifdef H5
    uni.chooseFile({
      count,
      type,
      extension,
      success: resolve,
      fail: reject
    })
    // #endif
  })
}

// 获取设备信息
export function getDeviceInfo() {
  const systemInfo = uni.getSystemInfoSync()
  return {
    brand: systemInfo.brand,
    model: systemInfo.model,
    system: systemInfo.system,
    version: systemInfo.version,
    screenWidth: systemInfo.screenWidth,
    screenHeight: systemInfo.screenHeight,
    windowWidth: systemInfo.windowWidth,
    windowHeight: systemInfo.windowHeight,
    statusBarHeight: systemInfo.statusBarHeight,
    platform: systemInfo.platform,
    pixelRatio: systemInfo.pixelRatio
  }
}

// 平台特定的样式适配
export function getPlatformStyles() {
  const { isWechatMP, isApp, isH5 } = getPlatform()
  
  return {
    // 导航栏高度适配
    navBarHeight: isApp ? 88 : 64,
    // 底部安全区域
    safeAreaBottom: isApp ? 34 : 0,
    // 状态栏高度
    statusBarHeight: uni.getSystemInfoSync().statusBarHeight || 20
  }
}